package com.yz.crm.department.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yz.common.util.JDKStackTrace;
import com.yz.crm.db.model.BaseCompany;
import com.yz.crm.db.model.BaseDepartment;
import com.yz.crm.department.model.DeptTreeNode;
import com.yz.crm.department.service.DepartmentService;
import com.yz.crm.passport.constant.PassportConst;
import com.yz.crm.passport.model.SessionUser;
import com.yz.util.StringUtil;
import com.yz.util.WebUtils;

/**
 * 部门管理action
 */
@Controller("departmentAction")
@RequestMapping(value="/admin/department")
public class DepartmentAction{
	
	private static Log log = LogFactory.getLog(DepartmentAction.class);
	
	@Resource(name="departmentService")
	private DepartmentService departmentService;

	@RequestMapping("/index")
	public String index (HttpServletRequest request,String deptId,HttpServletResponse response, ModelMap model){
        SessionUser user=WebUtils.getLoginUser(request);
        BaseCompany company=user.getCompany();
		model.put("company", company);
		if(StringUtils.isEmpty(deptId)){
			List<BaseDepartment> topList=this.departmentService.listTopDepartment(company.getId());
			model.put("deptList", topList);
		}else{
			List<BaseDepartment> subList=this.departmentService.listDirectSub(deptId);
			model.put("deptList", subList);
		}
		model.addAttribute("parentId", deptId);
		return "/admin/department/index";
	}
	
	/**
	 * 进入部门下级部门列表页
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value="/list")
	public String listDepartment(String deptId,HttpServletRequest request, ModelMap model){
		SessionUser user=WebUtils.getLoginUser(request);
		BaseCompany company=user.getCompany();
		model.put("company", company);
		if(StringUtils.isEmpty(deptId)){
			List<BaseDepartment> topList=this.departmentService.listTopDepartment(company.getId());
			model.put("deptList", topList);
			model.addAttribute("parentId", company.getId());
		}else{
			List<BaseDepartment> subList=this.departmentService.listDirectSub(deptId);
			model.put("deptList", subList);
			model.addAttribute("parentId", deptId);
		}
		return "/admin/department/list";
	}
	
	/**
	 * 保存部门
	 * @param dept
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/save")
	public @ResponseBody Map<String,Object> save(BaseDepartment dept,HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		
		Map<String,Object> saveMap=new HashMap<String,Object>();
		dept.setCode(StringUtil.trim(dept.getCode()));
		dept.setName(StringUtil.trim(dept.getName()));
		try {
			saveMap=this.departmentService.save(dept);
		} catch (Exception e) {
			log.error(JDKStackTrace.getJDKStrack(e));
			saveMap.put(PassportConst.OPER_SUCCESS, false);
			saveMap.put(PassportConst.OPER_MSG, "系统异常");
		}
		return saveMap;
	}
	
	/**
	 * 根据登录用户的所属公司取得部门树
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value="/tree")
	public String tree(HttpServletRequest request, ModelMap model){
		SessionUser user=WebUtils.getLoginUser(request);
		BaseCompany company=user.getCompany();
		model.put("company", company);
		return "/public/selectDept";
	}
	
	/**
	 * 加载下一级部门
	 * */
	@RequestMapping("/loadNext")
	@ResponseBody
	public List<DeptTreeNode> loadNext(String id,HttpServletRequest request, ModelMap modelMap){
		
		SessionUser user=WebUtils.getLoginUser(request);
		BaseCompany company=user.getCompany();
		
		List<BaseDepartment> deptList;
		if (company.getId().equals(id)){
			deptList = departmentService.listTopDepartment(id);
		}else{
			deptList = departmentService.listDirectSub(id);
		}
		
		List<DeptTreeNode> nodeList = new ArrayList<DeptTreeNode>();
		
		DeptTreeNode node;
		for (BaseDepartment c : deptList){
			node = new DeptTreeNode();
			node.setId(c.getId());
			node.setName(c.getName());
			node.setpId(c.getParentId());
			node.setParent(!departmentService.listDirectSub(c.getId()).isEmpty());
			nodeList.add(node);
		}
		return nodeList;
	}
	
	/**
	 * 删除部门
	 * @param deptId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete")
	public @ResponseBody Map<String,Object> delete(String deptId,HttpServletRequest request){
		Map<String,Object> saveMap=new HashMap<String,Object>();
		try{
			saveMap=this.departmentService.delete(deptId);
		}catch(Exception e){
			log.error(JDKStackTrace.getJDKStrack(e));
			saveMap.put(PassportConst.OPER_SUCCESS, false);
			saveMap.put(PassportConst.OPER_MSG, "系统异常");
		}
		return saveMap;
	}
}
